package com.ja.openchat.ui.chat.adapter.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.ShowLocationActivity;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.location.MsgLocationInfo;
import com.zy.openchat.core.model.message.UiMessage;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.zy.openchat.Constants;
import com.zy.openchat.util.DensityUtil;
import com.zy.openchat.util.glide.GlideApp;
import com.ja.openchat.views.BubbleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@MessageContentType(TelegramManager.MessageType.MessageType_Location)
@EnableContextMenu
public class LocationMessageContentViewHolder extends NormalMessageContentViewHolder {

    @BindView(R.id.imageView)
    BubbleImageView imageView;
    @BindView(R.id.location_title)
    TextView location_title;
    @BindView(R.id.location_content)
    TextView location_content;

    public LocationMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    public static int MESSAGE_CELL_LOCATION_WIDTH = 0;
    public static int MESSAGE_CELL_LOCATION_HEIGHT = 0;
    @Override
    public void onBind(UiMessage message) {
        if(message.message==null||message.message.content==null||message.message.content.location==null){
            return;
        }
        if(MESSAGE_CELL_LOCATION_WIDTH<=0){
            MESSAGE_CELL_LOCATION_WIDTH = DensityUtil.dp2px(200);
        }
        if(MESSAGE_CELL_LOCATION_HEIGHT<=0){
            MESSAGE_CELL_LOCATION_HEIGHT = DensityUtil.dp2px(120);
        }
        MsgLocationInfo locationInfo = message.message.content.location;


        String url = String.format("https://restapi.amap.com/v3/staticmap?location=%f,%f&zoom=17&size=%d*%d&markers=mid,,A:%f,%f&key=%s"
                ,locationInfo.longitude,locationInfo.latitude,MESSAGE_CELL_LOCATION_WIDTH, MESSAGE_CELL_LOCATION_HEIGHT
                ,locationInfo.longitude,locationInfo.latitude, Constants.GdWebMapAppId);

        encodeLonLat(locationInfo);
        doSearchQuery(locationInfo);
        GlideApp
            .with(fragment)
            .load(url)
            .into(imageView);

    }

    private void encodeLonLat(MsgLocationInfo locationInfo){
        if(!TextUtils.isEmpty(locationInfo.formatAddress)){
            location_content.setText(locationInfo.formatAddress);
            return;
        }
        try {
            GeocodeSearch geocoderSearch = new GeocodeSearch(fragment.getContext());
            geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener(){

                @Override
                public void onGeocodeSearched(GeocodeResult result, int rCode) {
                }

                @Override
                public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
                    locationInfo.formatAddress = result.getRegeocodeAddress().getFormatAddress();
                    location_content.setText(locationInfo.formatAddress);

                }});
            LatLonPoint lp = new LatLonPoint(locationInfo.latitude,locationInfo.longitude);
            RegeocodeQuery query = new RegeocodeQuery(lp, 200,GeocodeSearch.AMAP);
            geocoderSearch.getFromLocationAsyn(query);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static PoiSearch.Query query = new PoiSearch.Query("", "", "");

    static {
        query.setPageSize(1);
        query.setPageNum(0);
    }

    private void doSearchQuery(MsgLocationInfo locationInfo) {
        if(!TextUtils.isEmpty(locationInfo.title)){
            location_title.setText(locationInfo.title);
            return;
        }
        try {
            PoiSearch poisearch = new PoiSearch(fragment.getContext(), query);
            poisearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                @Override
                public void onPoiSearched(PoiResult poiResult, int resultCode) {
                    if (resultCode == AMapException.CODE_AMAP_SUCCESS) {
                        if (poiResult != null && poiResult.getPois().size() > 0) {
                            List<PoiItem> poiItems = poiResult.getPois();
                            locationInfo.title = poiItems.get(0).getTitle();
                            location_title.setText(locationInfo.title);
                        }
                    }
                }

                @Override
                public void onPoiItemSearched(PoiItem poiItem, int i) {

                }
            });
            poisearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(locationInfo.latitude,locationInfo.longitude), 500, true));
            poisearch.searchPOIAsyn();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClick(R.id.imageView)
    public void onClick(View view) {
        if(message.message==null||message.message.content==null||message.message.content.location==null){
            return;
        }
        ShowLocationActivity.startActivity(fragment.getContext(),message.message);
    }
}
